package whf.framework.meta.web;

import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import whf.framework.entity.AbstractEntity;
import whf.framework.meta.MetaFactory;
import whf.framework.meta.entity.Object;
import whf.framework.meta.entity.View;
import whf.framework.meta.service.ViewService;
import whf.framework.meta.service.ViewServiceImp;
import whf.framework.service.Service;
import whf.framework.util.BeanFactory;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.Constants;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-05-31
 */
public class ViewAction extends WebDispatchAction {

	@Override
	public WebForward list(AbstractEntity webEntity, WebData data) throws Exception {
		List list = super.getService().find("select distinct t.subModule from " + Object.class.getName() +" t order by t.subModule");
		data.setAttribute("submodules", list);
		webEntity.setWebMethod("list");
		return super.list(webEntity, data);
	}

	@Override
	public WebForward prepareUpdate(AbstractEntity webEntity, WebData data) throws Exception {
		String code = webEntity.getCode();
		if(!StringUtils.isEmpty(code)) {
			List tmp = super.getService().findWithoutLazy("t.code=?", new String[]{code});
			View view = null;
			if(tmp.size() > 0){
				view = (View)tmp.get(0);
			}
			if(view != null){
				view.setWebMethod("update");
				data.setAttribute(view);
				return data.forward("edit", Constants.G_EDIT_FORWARD);
			}
		}
		return super.prepareUpdate(webEntity, data);
	}

	@Override
	public WebForward update(AbstractEntity webEntity, WebData data) throws Exception {
		super.update(webEntity, data);
		return prepareUpdate(webEntity, data);
	}

	/* (non-Javadoc)
	 * @see whf.framework.web.struts.BaseDispatchAction#duplicate(org.apache.struts.action.ActionMapping, whf.framework.entity.AbstractEntity, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public WebForward duplicate(AbstractEntity webEntity, WebData data) throws Exception {
		ViewService service = (ViewService)super.getService();
		View source = (View)service.findByPrimaryKeyWithoutLazy(webEntity.getId());
		View view = (View)service.duplicate(source);
		data.setAttribute(view);
		return prepareUpdate(webEntity, data);
	}
	
	/**
	 * 预览
	 * @modify wanghaifeng Jul 3, 2007 10:31:47 AM
	 * @param mapping
	 * @param webEntity
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public WebForward preview(AbstractEntity webEntity, WebData data) throws Exception {
		long viewId = webEntity.getId();
		String viewCode = data.getParameter("view");
		View view = null;
		if(viewId > 0){
			view = ViewServiceImp.getViewService().findByPrimaryKey(viewId);
		} else {
			view = (View)ViewServiceImp.getViewService().findByCode(viewCode);
		}
		data.setAttribute("viewCode", view.getCode());
		data.setAttribute("title", view.getTitle());
		String boClassName = view.getReferencedObject().getBoClassName();
		data.setAttribute("boClassName", boClassName);
		
		Service service = BeanFactory.getService(MetaFactory.findByModelClass(Utils.getClassByClassName(boClassName)));
		List list = service.list("", 0, 20);
		//request.setAttribute("TEST-DATASOURCE", list);
		data.setAttribute("title", view.getName());
		return data.forward("preview");
	}
	
	public WebForward export(AbstractEntity webEntity, WebData data) throws Exception {
		long id = webEntity.getId();
		View view = ViewServiceImp.getViewService().findByPrimaryKeyWithoutLazy(id);
		Element ele = view.toXmlElement();
		Document doc = new Document(ele);

		HttpServletResponse res = data.getResponse();
		res.setContentType("text/xml;charset=UTF-8");
		OutputStream out = res.getOutputStream();
		XMLOutputter outputer = new XMLOutputter();
		outputer.output(doc, out);
		
		return null;
	}
} 